package xxx.demo.service.impl;

import java.util.List;

import xxx.demo.service.Demo;
import xxx.demo.service.DemoQuery;
import xxx.demo.service.DemoService;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import xxx.demo.dao.DemoDao;

/**
 * 演示模块业务接口实现
 * @gen CodeGen v1.1.0
 * @author LiuHG
 */
@Service
public class DemoServiceImpl implements DemoService{
	
    @Autowired
    private DemoDao demoDao;
	
        
        
	public void addDemo(Demo demo){
        
        
        demoDao.addDemo(demo);

	}
	
        
        
	public void updateDemo(Demo demo){
        demoDao.updateDemo(demo);

	}
	
        
        
	public void deleteDemo(String[] ids){
        demoDao.deleteDemo(ids);
	}
	
	public Demo getDemo(String id){
        return demoDao.getDemo(id);
	}
	
	public List<Demo> listDemo(DemoQuery query){
        return demoDao.listDemo(query);
	}
	
	@Override
	@org.springframework.transaction.annotation.Transactional
	public void moveToRow(String parentID,int targetRow, String sourceID) {
		String id = demoDao.getIdByRow(parentID, targetRow);
		moveToRow(parentID,id,sourceID);
	}
	
	@Override
	@org.springframework.transaction.annotation.Transactional
	public void moveToRow(String parentID,String targetID, String sourceID) {
		//如果targetID==sourceID直接返回
		if(targetID.equals(sourceID)) {
			return;
		}
		//按照序号字段排序，查询targetID对应的行号tn
		Integer tn = demoDao.getOrderNum(targetID);
		//按照序号字段排序，查询sourceID对应的行号sn
		Integer sn = demoDao.getOrderNum(sourceID);
		
		//如果tn>sn（向下移动），更新大于等于tn行号的记录全部+1
		if(tn.intValue() > sn.intValue()) {
			demoDao.increaseOrderNum(parentID, tn, -1);
		//如果tn<sn（向上移动），更新大于等于tn且小于等于sn行号的记录全部+1
		}else if(tn.intValue() < sn.intValue()){
			demoDao.increaseOrderNum(parentID, tn, sn);
		}
		
		//更新sourceID记录的序号为tn
		demoDao.updateOrderNum(sourceID, tn);
	}
}